<?php
class wms_mdl_expressarapi extends dbeav_model
{
	var $has_export_cnf = true;
    public $export_name = '应收快递费过数';

    function getselectdata($filter, $offset, $limit)
    {
        $list = $this->getList('*', $filter, $offset, $limit);
        $arapiMdl = app::get('wms')->model('expressarapi_line');
        $listData=array();
        $i=0;
        foreach ($list as $k => $v) 
        {
            $arapiata = $arapiMdl->getList("*",array('arapi_id'=>$v['id']));
            foreach ($arapiata as $key => $value) 
            {
                $listData[$i]['arapi_bn'] = ($v['arapi_bn'] ? $v['arapi_bn'] : '-');
                $listData[$i]['begin_date'] = ($v['begin_date'] ? $v['begin_date'] : '-');
                $listData[$i]['end_date'] = ($v['end_date'] ? $v['end_date'] : '-');
                $listData[$i]['create_time'] = ($v['create_time'] ? $v['create_time'] : '-');
                $listData[$i]['remark'] = ($v['remark'] ? $v['remark'] : '-');

                $listData[$i]['AXCustomer'] = ($value['AXCustomer'] ? $value['AXCustomer'] : '-');
                $listData[$i]['customername'] = ($value['customername'] ? $value['customername'] : '-');
                $listData[$i]['bn'] =  ($value['bn'] ? $value['bn'] : '-');
                $listData[$i]['product_name'] = ($value['product_name'] ? $value['product_name'] : '-');
                $listData[$i]['unit'] =  ($value['unit'] ? $value['unit'] : '-');
                $listData[$i]['saleqty'] =  ($value['saleqty'] ? $value['saleqty'] : '-');
                $listData[$i]['salemny'] =  ($value['salemny'] ? $value['salemny'] : '-');
                $listData[$i]['lmfee'] =  ($value['lmfee'] ? $value['lmfee'] : '-');
                $i=$i+1;
            }
        }
        return $listData;
    }




    function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end)
    {
        if($curr_sheet == 1){
            $title = array();
            foreach ($this->io_title('apapi') as $k => $v) {
                $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
            }
            $data['content']['main'][0] = '"' . implode('","', $title) . '"';
        }
        $list = $this->getselectdata($filter, $start, $end);
        foreach ($list as $key => $value) 
        {
            $list[$key]['create_time'] = $value['create_time']?date('Y-m-d H:i:s', $value['create_time']):'';
            $list[$key]['begin_date'] = $value['begin_date']?date('Y-m-d H:i:s', $value['begin_date']):'';
            $list[$key]['end_date'] = $value['end_date']?date('Y-m-d H:i:s', $value['end_date']):'';
        }
        foreach ($list as $k => $aFilter) {
            $exptmp_data = array();
            foreach ($aFilter as $col => $val) {
                $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                $exptmp_data[] = $aFilter[$col];
            }
            $data['content']['main'][] = implode(',', $exptmp_data);
        }
        return $data;
    }
    function io_title($filter, $ioType = 'csv')
    {
        switch ($filter) {
            case 'apapi':
                $this->oSchema['csv'][$filter] = array(
                    '*:过数单号' => 'arapi_bn',
                    '*:开始日期' => 'begin_date',
                    '*:结束日期' => 'end_date',
                    '*:创建时间' => 'create_time',
                    '*:备注' => 'remark',
                    '*:客户' => 'AXCustomer',
                    '*:客户名称' => 'customername',
                    '*:料号' => 'bn',
                    '*:料品	' => 'product_name',
                    '*:单位	' => 'unit',
                    '*:销售数量 ' => 'saleqty',
                    '*:销售金额' => 'salemny',
                    '*:应收运费' => 'lmfee',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys($this->oSchema[$ioType][$filter]);
        return $this->ioTitle[$ioType][$filter];
    }



    public function get_schema_export()
    {
        $schema = array(
            'columns' => array(
                'arapi_bn' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '过数单号',
                    'in_list' => true,
                    'default_in_list' => true,
                    'searchtype' => 'has',
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'width' => 150,
                    'order' => 20
                ),
                'begin_date' => array(
                    'type' => 'time',
                    'label'=>'开始日期',
                    'in_list' => false,
                    'default_in_list' => false,
                    'order' => 130,
                ),
                'end_date' => array(
                    'type' => 'time',
                    'label'=>'结束日期',
                    'in_list' => false,
                    'default_in_list' => false,
                    'order' => 130,
                ),
                'create_time' => array(
                    'type' => 'time',
                    'label'=>'创建时间',
                    'in_list' => false,
                    'default_in_list' => false,
                    'order' => 130,
                ),
                'remark' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '备注',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'AXCustomer' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '客户',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'customername' => array (
                    'type' => 'varchar(80)',
                    'default' => 0,
                    'required' => false,
                    'label' => '客户名称',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 65,
                    'order' => 50
                ),
                'bn' => array (
                    'type' => 'varchar(32)',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '料号',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 50,
                    'order' => 70,
                ),
                'product_name' => array (
                    'type' => 'varchar(200)',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '产品名称',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 65,
                    'order' => 70,
                ),
                'unit' => array (
                    'type' => 'varchar(20)',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '单位',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 65,
                    'order' => 75,
                ),
                'salemny' => array(
                    'type' => 'money',
                    'label'=>'销售金额',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 100,
                    'order' => 100,
                ),
                'saleqty' => array (
                    'type' => 'number',
                    'default' => 0,
                    'required' => false,
                    'editable' => true,
                    'label' => '销售数量',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 80,
                ),
                'lmfee' => array(
                    'type' => 'money',
                    'label'=>'应收运费',
                    'in_list' => true,
                    'default_in_list' => true,
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'order' => 120,
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array(
                0 => 'apapi_bn',
                1 => 'begin_date',
                2 => 'end_date',
                3 => 'create_time',
                4 => 'remark',
                5 => 'AXsupplierCode',
                6 => 'JSType',
                7 => 'bn',
                8 => 'product_name',
                9 => 'unit',
                10 => 'salemny',
                11 => 'saleqty',
                12 => 'lmfee',
            ),
            'default_in_list' => array(
                0 => 'apapi_bn',
                1 => 'begin_date',
                2 => 'end_date',
                3 => 'create_time',
                4 => 'remark',
                5 => 'AXsupplierCode',
                6 => 'JSType',
                7 => 'bn',
                8 => 'product_name',
                9 => 'unit',
                10 => 'salemny',
                11 => 'saleqty',
                12 => 'lmfee',
            ),
        );
        return $schema;
    }



    public function getExportTitle($fields)
    {
        $title = array();
        $main_columns = $this->get_schema_export();
        foreach (explode(',', $fields) as $k => $col) {
            if (isset($main_columns['columns'][$col])) {
                $title[] = "*:" . $main_columns['columns'][$col]['label'];
            }
        }

        return mb_convert_encoding(implode(',', $title), 'GBK', 'UTF-8');
    }
}